'use strict';

const Controller = require('egg').Controller;
class PageListController extends Controller {
    // 获取文章列表

    async pageLists() {
        const { ctx } = this;
        const { pageCode, pageSize, title, url, status } = ctx.request.body;
        // console.log(user,'11')
        // console.log(status, 'kkkka')

        let pageLists = await this.app.mysql.select('pageLists');
        // console.log(lists.length)
        let newLists = pageLists;
        // console.log(num,'ii')
        if (title) {
            newLists = newLists.filter(v => v.title.includes(title))
        }
        if (url) {
            newLists = newLists.filter(v => v.url.includes(url))
        }
        if (status) {
            newLists = newLists.filter(v => v.state == status)
        }
        // console.log(num, 'kk')
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        // console.log(newLists, 'ff')

        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除文章列表
    async deletePageLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('pageLists', { id });
        if (result) {
            const list = await this.app.mysql.select('pageLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }




    // 多选删除
    async pageDel() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        console.log(multipleSelection, 'lll')
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('pageLists', { id: v.id })
        });
        console.log(chooseLists, 'jjj')
        if (chooseLists) {
            const list = await this.app.mysql.select('pageLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }

    //批量删除
    async pageDel() {
        const { ctx } = this;
        const { id } = ctx.request.body
        console.log(id,'====')
        let sql = `DELETE FROM pageLists WHERE id in (${id})`

        try {
            const results = await this.app.mysql.query(sql)
            ctx.body = {
                code: 200,
                msg: '删除成功',
            }
        } catch (err) {
            ctx.body = {
                code: 505,
                msg: '服务器繁忙，请稍后重试',
                err,
            }
        }
    }

    // 添加文章
    async addPageLists() {
        const { ctx } = this;
        const { title, classes, tag } = ctx.request.body;
        let newTag = JSON.stringify(tag)

        const result = await this.app.mysql.insert('pageLists',
            { title, orders:0, url:'http://dkkfd',state: 'false', readers: 0,  createtime: new Date().toLocaleString('Chinese', { hour12: false }) });
        if (result) {
            let list = await this.app.mysql.select('pageLists');
            ctx.body = {
                code: 200,
                msg: '添加成功',
                list
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '添加失败',
            }
        }
    }

    // 发布状态
    async pagePublish() {
        const { ctx } = this;
        const { multipleSelection } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.update('pageLists', { id: v.id, state: 'true' })

        });

        if (chooseLists) {
            const list = await this.app.mysql.select('pageLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '发布成功',
                    list,
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '发布失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '发布错误',
            }
        }


    }
    async editPageLists() {
        const { ctx } = this
        const { title,  id } = ctx.request.body;
        const row = {
            title,
            state: 'false',    // any other fields u want to update
            orders:23,
            readers: 0,
            createtime: new Date().toLocaleString('Chinese', { hour12: false }),
            
        };

        const options = {
            where: {
                id: id
            }
        };
        //    console.log(id,title,'ddfdf')
        const result = await this.app.mysql.update('pageLists', row, options);
        if (result) {
            let list = await this.app.mysql.select('pageLists');
            ctx.body = {
                code: 200,
                msg: '编辑成功',
                list
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '编辑失败',
            }
        }
    }

    // 草稿
    async pageDraft() {
        const { ctx } = this;
        const { multipleSelection } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.update('pageLists', { id: v.id, state: 'false' })

        });

        if (chooseLists) {
            const list = await this.app.mysql.select('pageLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '草稿成功',
                    list,
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '草稿失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '草稿错误',
            }
        }
    }
    // 通过
    // async through() {
    //     const { ctx } = this;
    //     const { id } = ctx.request.body;
    //     // const chooseLists = multipleSelection.map(v => {
    //     //     return this.app.mysql.update('pageLists', { id: v.id, state: 'false' })

    //     // });
    //     const row = {
    //         state: 'true'
    //     };

    //     const options = {
    //         where: {
    //             id
    //         }
    //     };
    //     const result = await this.app.mysql.update('pageLists', row, options); // 更新 posts 表中的记录

    //     if (result) {
    //         const list = await this.app.mysql.select('commentLists');
    //         ctx.body = {
    //             code: 200,
    //             msg: '通过成功',
    //             list,
    //         }
    //     } else {
    //         ctx.body = {
    //             code: 404,
    //             msg: '通过失败',
    //         }
    //     }



    // }

    // 拒绝
    // async refused() {
    //     const { ctx } = this;
    //     const { id } = ctx.request.body;
    //     // const chooseLists = multipleSelection.map(v => {
    //     //     return this.app.mysql.update('pageLists', { id: v.id, state: 'false' })

    //     // });
    //     const row = {
    //         state: 'false'
    //     };

    //     const options = {
    //         where: {
    //             id
    //         }
    //     };
    //     const result = await this.app.mysql.update('commentLists', row, options); // 更新 posts 表中的记录

    //     if (result) {
    //         const list = await this.app.mysql.select('commentLists');
    //         ctx.body = {
    //             code: 200,
    //             msg: '拒绝成功',
    //             list,
    //         }
    //     } else {
    //         ctx.body = {
    //             code: 404,
    //             msg: '拒绝失败',
    //         }
    //     }

    // }


}

module.exports = PageListController;
